package com.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.pojo.User;

public class UserAction extends ActionSupport implements ModelDriven<User> {

//1. 基本数据类型字段驱动方式的数据传递
    private String username; // 用户名
    private String password; // 密码
    // username的getter和setter方法
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    // password的getter和setter方法
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public String login(){
        ActionContext context = ActionContext.getContext();
        if("admin".equals(username) && "123456".equals(password)){
            context.put("username", username);
            context.put("password", password);
            context.put("success","登录成功！");
            return SUCCESS;
        }else{
            context.put("error","用户名或者密码错误，请重新登录！");
            return ERROR;
        }
    }


//2. 直接使用域对象字段驱动方式的数据传递
    public String login_(){
        ActionContext context = ActionContext.getContext();
        if("admin".equals(user.getUsername()) && "123456".equals(user.getPassword())){
            context.put("username", user.getUsername());
            context.put("password", user.getPassword());
            context.put("success","登录成功！");
            return SUCCESS;
        }else{
            context.put("error","用户名或者密码错误，请重新登录！");
            return ERROR;
        }
    }

    private User user = new User();
    @Override
    public User getModel() {
        return user;
    }


    public String loginBook(){
        ActionContext context = ActionContext.getContext();
        if("admin".equals(user.getUsername()) && "123456".equals(user.getPassword())){
            context.put("username", user.getUsername());
            context.put("password", user.getPassword());
            context.put("success","登录成功！");
            context.getSession().put("user",user);
            return SUCCESS;
        }else{
            context.put("error","用户名或者密码错误，请重新登录！");
            return ERROR;
        }
    }

    public String buy(){
        return SUCCESS;
    }
}
